package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntStageInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntStageInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class MchntStageInfoMapperImpl implements MchntStageInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntStageInfo(MchntStageInfo mchntStageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntStageInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntStageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户分期信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntStageInfo.getInstId(), mchntStageInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntStageInfoById(MchntStageInfo mchntStageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntStageInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntStageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户分期信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntStageInfo.getInstId(), mchntStageInfo.getMchntNo(), mchntStageInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntStageInfo selectMchntStageInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoById");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntStageInfo, mchntStageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户分期信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntStageInfo;
    }

    @Override
    public MchntStageInfo selectMchntStageInfoByPrimaryKey(String instId, String mchntNo, String baseProdCode, String prodStageCode, int mchntStageNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoByPrimaryKey");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setMchntNo(mchntNo);
        mchntStageInfo.setBaseProdCode(baseProdCode);
        mchntStageInfo.setProdStageCode(prodStageCode);
        mchntStageInfo.setMchntStageNum(mchntStageNum);
        mchntStageInfo.setFeeEffectiveDate(DateUtil.getCurrentDate());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntStageInfo, mchntStageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户分期信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,prodStageCode:<{}>,mchntStageNum:<{}>,iReturnCode:<{}>", instId, mchntNo, baseProdCode, prodStageCode, mchntStageNum, iReturnCode);
            return null;
        }
        return mchntStageInfo;
    }

    @Override
    public List<MchntStageInfo> selectMchntStageInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoByMchntNo");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntStageInfo, MchntStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户分期信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntStageInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntStageInfo> selectMchntStageInfoByProdStageCode(String instId, String mchntNo, String prodStageCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoByProdStageCode");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setMchntNo(mchntNo);
        mchntStageInfo.setProdStageCode(prodStageCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntStageInfo, MchntStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户分期信息失败,instId:<{}>,mchntNo:<{}>,prodStageCode:<{}>", instId, mchntNo, prodStageCode);
            return null;
        }
        return objectList.stream().map(item -> (MchntStageInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<MchntStageInfo> selectMchntStageInfoByBaseProdCode(String instId, String mchntNo, String baseProdCode, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoByBaseProdCode");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setMchntNo(mchntNo);
        mchntStageInfo.setBaseProdCode(baseProdCode);
        mchntStageInfo.setProdCode(prodCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntStageInfo, MchntStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户分期信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>", instId, mchntNo, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (MchntStageInfo) item).collect(Collectors.toList());
    }

    @Override
    public MchntStageInfo selectMchntStageInfoByStagePrimaryKey(String instId, String stagePrimaryKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntStageInfoByStagePrimaryKey");
        MchntStageInfo mchntStageInfo = new MchntStageInfo();
        mchntStageInfo.setInstId(instId);
        mchntStageInfo.setStagePrimaryKey(stagePrimaryKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntStageInfo, mchntStageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户分期信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,prodStageCode:<{}>,mchntStageNum:<{}>,iReturnCode:<{}>", instId, stagePrimaryKey, iReturnCode);
            return null;
        }
        return mchntStageInfo;
    }
}
